


import numpy as np
import math as m

# 5*5
class DEMATEL:
    
    # 赋值
    def __init__(self, DecisionMatrix,*waste1,**waste2):
        
        self.Matrix=DecisionMatrix
        
    
    def getResult(self):
        relnumber=self.getMarginalUtilityScore(self.Matrix)

        KIndex=self.getKIndex(relnumber)
        DMatrix=self.getDMatrix(KIndex,relnumber)
        TMatrix=self.getTMatrix(DMatrix)
        r=self.R(TMatrix)
        c=self.C(TMatrix)
        result=self.getR(r,c,TMatrix)
        result=[ i[1] for i in result]
        result=[i/sum(result) for i in result]
        return result


    def getMarginalUtilityScore(self,NormalizedMatrix):
            '''
            获取边际效用得分
            :param NormalizedMatrix:规范化矩阵
            :return:
            '''
            ScoreMatrix=[]
            for Row in range(len(NormalizedMatrix)):
                Scoreline=[]
                for Column in range(len(NormalizedMatrix[0])):
                    t=self.getscore(NormalizedMatrix[Row][Column])
                    Scoreline.append(t)
                ScoreMatrix.append(Scoreline)
            return ScoreMatrix
    def getscore(self,IVq):
            '''
            我们提出的得分函数
            :param IVq:区间值
            :param q:q值
            :return:
            '''
            a = IVq[0][0]
            b = IVq[0][1]
            c = IVq[1][0]
            d = IVq[1][1]
            return (m.log(a + b + c + d + 1) + (b - a) + (d - c) ** 2 + (a - c + b - d) * m.log(3) / 2) / (2 * m.log(3))


    def getKIndex(self,realNumbers):

        column=[]
        line=[]

        for index in range(5):

            sumcolumn=0

            for i in realNumbers:

                if index==0:

                    line.append(sum(i))
                    
                sumcolumn+=i[index]
            
            column.append(sumcolumn)
            
        temp=[max(column),max(line)]

        return 1/min(temp)

    def getDMatrix(self,k_index,Matrix):
        DMatrix=[]
        for i in range(len(Matrix)):
            list=[j*k_index for j in Matrix[i]]
            DMatrix.append(list)
            

        return DMatrix
        
    def getTMatrix(self,DMatrix):

        right=np.array(DMatrix)
        left=np.eye(len(DMatrix))-right

        return right*np.linalg.inv(left)

    def R(self,TMatrix):
        r=[]
        for i in TMatrix:
            r.append(sum(i))

        return r


    def C(self,TMatrix):
        c=[]
        for i in range(len(TMatrix)):
            sum=0
            for j in TMatrix:
                
                sum+=j[i]

            c.append(sum)

        return c


    def getR(self,r,c,TMatrix):

        sum=[i+j for i,j in zip(r,c)]
        dict={}
        for i in range(len(sum)):

            dict[i]=sum[i]

        
        result=sorted(dict.items(),key=lambda x:x[1])

        return result


if __name__ == "__main__":
    Matrix = np.array([
    [([0.58, 0.75], [0.1, 0.2]), ([0.7, 0.78], [0.2, 0.3]), ([0.5, 0.78], [0.1, 0.2]),([0.65, 0.7], [0.3, 0.35]), ([0.4, 0.5], [0.5, 0.6])],

    [([0.5, 0.6], [0.1, 0.2]), ([0.75, 0.78], [0.2, 0.3]), ([0.6, 0.78], [0.1, 0.15]),([0.7, 0.8], [0.2, 0.3]), ([0.5, 0.6], [0.4, 0.5])],

    [([0.5, 0.78], [0.1, 0.2]), ([0.6, 0.7], [0.3, 0.4]), ([0.7, 0.78], [0.2, 0.3]), ([0.6, 0.7], [0.3, 0.4]),([0.35, 0.45], [0.5, 0.65])],

    [([0.75, 0.78], [0.2, 0.3]), ([0.6, 0.75], [0.2, 0.3]), ([0.7, 0.78], [0.2, 0.3]),([0.55, 0.65], [0.3, 0.4]), ([0.35, 0.4], [0.6, 0.65])],

    [([0.6, 0.75], [0.3, 0.4]), ([0.5, 0.6], [0.4, 0.5]), ([0.6, 0.7], [0.3, 0.4]), ([0.5, 0.6], [0.4, 0.5]),([0.1, 0.2], [0.4, 0.5])]])

    dematel=DEMATEL(Matrix)  

    res=dematel.getResult()
    print(res)


   

